<?php
namespace app\manage\controller;
use think\Db;

class channelIssue extends Base
{
    //列表
    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $channelId = input('channel_id');
            $gameId = input('game_id');

            $where = [];
            !empty($channelId) && $where['channel_id'] = ['in', $channelId];
            !empty($gameId) && $where['game_id'] = ['in', $gameId];

            if( ! $count=model('channelGame')->where($where)->count() ){
                $this->layTable();
            }

            $select = model('channelGame')
                ->where($where)
                ->order('channel_id desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            $gameIdName = model('Game')->gameIdName();
            $channelIdName = model('Channel')->channelIdName();
            $packProcess = config('pack_process'); //打包进度
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'channel_id' => $v['channel_id'],
                    'channel_name' => isset($channelIdName[ $v['channel_id'] ]) ? $channelIdName[ $v['channel_id'] ] : '',
                    'game_id' => $v['game_id'],
                    'game_name' => isset($gameIdName[ $v['game_id'] ]) ? $gameIdName[ $v['game_id'] ] : '',
                    'download' => $v['download'],
                    'status' => $packProcess[ $v['status'] ],
                ];
            }
            $this->layTable(0, '打包列表', $data, $count);
        }else{
            $channelData = model('ViewChannel')->field('cps_id,cps_name')->order('cps_id desc')->where(['channel_id'=>1000])->select();
            $gameData = model('ViewGameManage')->field('game_id,game_name')->order('game_id desc')->select();
            return view('', ['channel_data'=>$channelData, 'game_data'=>$gameData]);
        }
    }

    //删除
    public function pack(){
        $channelAll = input('channel_all');
        $gameAll = input('game_all');

        if(empty($channelAll) || empty($gameAll)){
            $this->layJson(1, '请选择渠道和游戏');
        }

        $channelIdArr = explode(',', $channelAll);
        $gameIdArr = explode(',', $gameAll);

        $gameIdUp = changeValKey('ViewGameManage', 'game_id', 'up_location');
        $gameIdCode = changeValKey('ViewGameManage', 'game_id', 'code');

        //根据所选的多个渠道进行遍历,
        foreach ($channelIdArr as $k => $cid) {
            foreach ($gameIdArr as $k2 => $gid) {
                //如果已经有该渠道和游戏的打包队列，则该记录清除状态
                $where['channel_id'] = $cid;
                $where['game_id'] = $gid;

                if( model('ChannelGame')->where($where)->count() ){
                    $save['addtime'] = time();
                    $save['status'] = 0;
                    $save['altertime'] = time();
                    $save['msg'] = '排队中';
                    $save['packcount'] = 0;
                    $save['download'] = '';
                    Db::name('ChannelGame')->where($where)->update($save);
                    continue;
                }

                //没有该渠道和游戏的打包队列，则新增
                $add['channel_id'] = $cid;
                $add['game_id'] = $gid;
                $add['game_code'] = $gameIdCode[ $gid ];
                $add['addtime'] = time();
                $add['status'] = 0;
                $add['altertime'] = time();
                $add['msg'] = '排队中';
                $add['packcount'] = 0;
                $add['download'] = '';
                $add['storage'] = $gameIdUp[ $gid ];

                Db::name('ChannelGame')->insert($add);
            }
        }
        $this->layJson(0, '成功加入打包队列');
    }
}